﻿using ScottPlot;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WpfDrawApp
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<double> XDataList = new List<double>() { 0, 1, 1, 301, 301, 311, 311, 521, 521, 531, 531, 621, 621, 631, 631, 668.8, 668.8, 669.8 };
        List<double> YDataList = new List<double>() { 0, 0, 161.695, 161.695, 0, 0, -88.6, -88.6, 0, 0, -26.6, -26.6, 0, 0, 161.695, 161.695, 0, 0 };

        public MainWindow()
        {
            InitializeComponent();

            this.Closing += CloseWindow;
            double[] XDataArray = XDataList.ToArray();
            double[] YDataArray = YDataList.ToArray();
            WpfPlot1.Plot.AddScatter(XDataArray, YDataArray);

            Thread refreshDataThread = new Thread(RefreshData);
            refreshDataThread.Start();

            WpfPlot1.Plot.XAxis.Label("Time(min)");
            WpfPlot1.Plot.YAxis.Label("P(W)");
            WpfPlot1.Plot.XAxis2.Label("line chart");
            WpfPlot1.Plot.YAxis2.Label("iteration");

            WpfPlot1.Refresh();
        }

        public void RefreshData()
        {
            List<double> XDataList2 = new List<double>() { 0, 1, 1, 301, 301, 311, 311, 521, 521, 531, 531, 621, 621, 631, 631, 668.8, 668.8, 669.8 }; ;
            List<double> YDataList2 = new List<double>() { 0, 0, 161.695, 161.695, 0, 0, -88.6, -88.6, 0, 0, -26.6, -26.6, 0, 0, 161.695, 161.695, 0, 0 };
            int t = 1;
            while (t < 670)
            {
                for (int i = 0; i < XDataList2.ToArray().Length; i++)
                {
                    XDataList2[i] = XDataList2[i] + 1;
                }
                for (int i = 0; i < YDataList2.ToArray().Length; i++)
                {
                    YDataList2[i] = YDataList2[i];
                }

                double[] XDataArray2 = XDataList2.ToArray();
                double[] YDataArray2 = YDataList2.ToArray();
                WpfPlot1.Plot.Clear();
                WpfPlot1.Plot.AddScatter(XDataList.ToArray(), YDataList.ToArray());
                WpfPlot1.Plot.AddScatter(XDataArray2, YDataArray2);
                WpfPlot1.Plot.XAxis2.Label(t.ToString());
                WpfPlot1.Plot.YAxis2.Label(t+"%");

                Dispatcher.Invoke(new Action(delegate
                {
                    WpfPlot1.Refresh();
                    WpfPlot1.Plot.AxisAutoY();
                    WpfPlot1.Plot.AxisAutoX();
                }));

                t++;
                Thread.Sleep(100);
            }
        }

        public void IterationData()
        {
            while (true)
            {
                List<double> XDataListIteration = XDataList;
                List<double> YDataListIteration = YDataList;
                for (int i = 0; i < XDataListIteration.ToArray().Length; i++)
                {
                    XDataListIteration[i] = XDataListIteration[i] + 310;
                }
                for (int i = 0; i < YDataListIteration.ToArray().Length; i++)
                {
                    YDataListIteration[i] = YDataListIteration[i];
                }

                double[] XDataArrayIteration = XDataListIteration.ToArray();
                double[] YDataArrayIteration = YDataListIteration.ToArray();
                WpfPlot1.Plot.AddScatter(XDataArrayIteration, YDataArrayIteration);

                Dispatcher.Invoke(new Action(delegate
                {
                    WpfPlot1.Refresh();
                    WpfPlot1.Plot.AxisAutoY();
                    WpfPlot1.Plot.AxisAutoX();
                }));
                Thread.Sleep(1000);
            }
        }

        private void CloseWindow(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Process.GetCurrentProcess().Kill();
            this.Close();
        }
    }
}
